package cn.bestick.weightrecord.controller;

import cn.bestick.weightrecord.dto.DataWrapper;
import cn.bestick.weightrecord.dto.WrapperUser;
import cn.bestick.weightrecord.model.User;
import cn.bestick.weightrecord.service.UserService;
import cn.bestick.weightrecord.util.INetUtil;
import cn.bestick.weightrecord.util.StatisticUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by IntelliJ IDEA.
 * User: Bestick
 * Date: 2016/7/9
 * Time: 12:31
 * Url: http://www.bestick.cn
 * Copyright © 2015-2016 Bestick All rights reserved
 * -----------------------------------------------------------
 * 会当凌绝顶，一览众山小。
 */
@Controller
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 登录接口
     *
     * @param user    页面自动封装user对象
     * @param session HttpSession
     * @return WrapperUser
     */
    @RequestMapping(value = "/login")
    public
    @ResponseBody
    WrapperUser login(User user, HttpSession session) {
        WrapperUser wrapperUser = userService.login(user);
        //如果登录失败，currentUser为null
        User currentUser = wrapperUser.getData();
        if (currentUser != null) {
            session.setAttribute("currentUser", currentUser);
            //当前用户加入OnLineUsers
            for (User u : StatisticUtil.getOnLineUsersInstance()) {
                if (u.getUserName().equals(currentUser.getUserName())) {
                    //遍历过程中一旦出来相同，则currentUser已存在于OnLineUsers中，无须添加。
                    return wrapperUser;
                }
            }
            //循环遍历完之后任没相同则把currentUser添加到OnLineUsers中
            StatisticUtil.getOnLineUsersInstance().add(currentUser);
        }
        return wrapperUser;
    }

    /**
     * 登出接口
     *
     * @param session HttpSession
     * @return DataWrapper
     */
    @RequestMapping(value = "/logout")
    public String logout(HttpSession session) {
        //当前用户从OnLineUsers中移除
        User currentUser = (User) session.getAttribute("currentUser");
        if (StatisticUtil.getOnLineUsersInstance().contains(currentUser)) {
            StatisticUtil.getOnLineUsersInstance().remove(currentUser);
        }
        session.removeAttribute("currentUser");
        return "redirect:/index.html";
    }

    /**
     * 用户注册
     *
     * @param user 页面自动封装user对象
     * @return DataWrapper
     */
    @RequestMapping(value = "/adduser")
    public
    @ResponseBody
    DataWrapper addUser(User user, HttpServletRequest request) {
        user.setIp(INetUtil.getIpAddress(request));
        return userService.addUser(user);
    }

    /**
     * 获取当前登录用户的信息
     *
     * @param session HttpSession
     * @return WrapperUser
     */
    @RequestMapping(value = "/getcurrentuserinfo")
    public
    @ResponseBody
    WrapperUser getCurrentUserInfo(HttpSession session) {
        User currentUser = (User) session.getAttribute("currentUser");
        return userService.getCurrentUserInfo(currentUser);
    }
}
